<template>
  <transition name="fade">
    <div>
      <div v-title :data-title="sysTitle"></div>
      <router-view></router-view>
    </div>
  </transition>
</template>

<script>
  import Utils from './utils/Uitls'
  import elementEnLocale from 'element-ui/lib/locale/lang/en'
  import elementZhLocale from 'element-ui/lib/locale/lang/zh-CN'
  import elementLocale from 'element-ui/lib/locale'

  export default {
    data () {
      return {
        sysTitle: this.$t('sys.name')
      }
    },
    created () {
      let self = this
      // 在这里统一设置网页语言
      // this.setCurLang()

      // 这里全局监听切换语言设置的消息
      // this.$bus.$on('switchLang',self.switchLang)
    },
    methods: {
      // 设置网页语言，默认：获取浏览器语言
      setCurLang () {
        let self = this

        // 若cookie中未保存语言设置，则取浏览器语言
        let cLang = self.$cookie.get('cur_lang')
        if (!cLang || cLang === undefined || cLang === '' || cLang === 'null') {
          cLang = Utils.getBrowserLang()
          self.$cookie.set('cur_lang', cLang) // 保存语言设置到cookie
        }

        if (cLang === 'zh' || cLang === 'zh-cn') {
          self.$i18n.locale = 'zh-cn'
          elementLocale.use(elementZhLocale)
        } else {
          // 非中文都设置为英文
          self.$i18n.locale = 'en'
          elementLocale.use(elementEnLocale)
        }

        // 更新网页title
        self.sysTitle = self.$t('sys.name')
      },
      // 切换网页语言
      switchLang (val) {
        let self = this
        let cLang = self.$cookie.get('cur_lang')
        if (cLang === 'zh-cn' || cLang === 'zh') {
          // 中文切换成英文
          self.$i18n.locale = 'en'
          elementLocale.use(elementEnLocale)
          self.$cookie.set('cur_lang', 'en')
        } else {
          // 非中文文切换成中文
          self.$i18n.locale = 'zh-cn'
          elementLocale.use(elementZhLocale)
          self.$cookie.set('cur_lang', 'zh-cn')
        }
        // 更新网页title
        self.sysTitle = self.$t('sys.name')
        window.location.reload()
      }
    }
  }
</script>
